﻿
Public Module _disposer
    Public Function make_disposer(Of T)(ByVal p As Func(Of T),
                                        Optional ByVal init As Action = Nothing,
                                        Optional ByVal disposer As Action(Of T) = Nothing) _
                                       As disposer(Of T)
        Return New disposer(Of T)(p, init, disposer)
    End Function

    Public Function make_disposer(Of T)(ByVal p As T,
                                        Optional ByVal init As Action = Nothing,
                                        Optional ByVal disposer As Action(Of T) = Nothing) _
                                       As disposer(Of T)
        Return New disposer(Of T)(p, init, disposer)
    End Function
End Module

Public Class disposer(Of T)
    Inherits dispose_ptr(Of T)
    Implements IDisposable

    Public Sub New(ByVal p As Func(Of T),
                   Optional ByVal init As Action = Nothing,
                   Optional ByVal disposer As Action(Of T) = Nothing)
        MyBase.New(p, init, disposer)
    End Sub

    Public Sub New(ByVal p As T,
                   Optional ByVal init As Action = Nothing,
                   Optional ByVal disposer As Action(Of T) = Nothing)
        MyBase.New(p, init, disposer)
    End Sub

    Public Sub New(Optional ByVal init As Action = Nothing,
                   Optional ByVal disposer As Action(Of T) = Nothing)
        MyBase.New(init, disposer)
    End Sub

    Public Shadows Sub dispose()
        MyBase.dispose()
        GC.SuppressFinalize(Me)
    End Sub

    Public Shadows Sub queue_dispose()
        MyBase.queue_dispose()
        GC.SuppressFinalize(Me)
    End Sub

    Public Sub IDisposable_Dispose() Implements IDisposable.Dispose
        dispose()
    End Sub

    Protected NotOverridable Overrides Sub Finalize()
        dispose()
        MyBase.Finalize()
    End Sub
End Class
